package com.kk.netty.basice.demo1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

public class Service {
    public static void main(String[] args) throws Exception {
        // 启动器，负责组装 netty 组件，启动服务器
        new ServerBootstrap ( )
                // BoosEventLoop ,WorkerEventLoop(selector,thread)
                .group (new NioEventLoopGroup ( ))
                // 选择服务器的 ServerSocketChannel 实现
                .channel (NioServerSocketChannel.class)
                // boos 负责处理连接， worker(child) 负责处理读写，
                .childHandler (
                        // channel 代表和客户端进行数据读写的通道，
                        // Initializer初始化
                        new ChannelInitializer<NioSocketChannel> ( ) {
                            // 建立连接后，调用初始化方法
                    protected void initChannel(NioSocketChannel ch) {
                        // 将 ByteBuf 还原为 hello...
                        ch.pipeline ( ).addLast (new StringDecoder ( ));
                        ch.pipeline ( ).addLast (new SimpleChannelInboundHandler<String> ( ) { // 6
                            @Override
                            // 执行 read方法，打印 hello...
                            protected void channelRead0(ChannelHandlerContext ctx, String msg) {
                                System.out.println (msg);
                            }
                        });
                    }
                    // 绑定监听端口
                }).bind (8080);
    }
}








